#include "Logger.h"
#include "LogManager.h"

Logger::Logger (LogManager *lm, const char *module_name)
: log_level_ (0)
, parent_llevel_ (0)
, module_name_ (0)
, next_node_ (0)		 
, log_manager_ (lm)
{
	int len = strlen (module_name);
	module_name_ = new char[MAX_MODULE_NAME_LEN + 1];
	memset (module_name_, '\0', MAX_MODULE_NAME_LEN + 1);
	strncpy (module_name_, module_name, len);
#ifdef NDK_LOG_DEBUG
	fprintf (stderr, "Construct Logger\n");
#endif
}
Logger::~Logger ()
{
	if (this->module_name_)
		delete []this->module_name_;
	this->module_name_ = 0;
}
#if 0
void Logger::update_lrecord_cfg (LogManager::LogRecord *lr_cfg)
{
	if (lr_cfg == 0) return;

	if (this->lrecord_cfg_)
	{
		int len = this->lrecord_cfg_->max_len_of_one_record_;
		*this->lrecord_cfg_ = *lr_cfg;
		if (len < lr_cfg->max_len_of_one_record_)
		{
			if (this->log_record_)
				delete []this->log_record_;
			this->log_record_ = 0;
		}else
		{
			return ;
		}
	}
	this->log_record_ = new char[this->lrecord_cfg_->max_len_of_one_record_ + 1];
	memset (this->log_record_, '\0', 
			this->lrecord_cfg_->max_len_of_one_record_ + 1);
	/*
	// Recursive.
	Logger *l = this->head_node_;
	if (l == 0) return;
	while (l)
	{
	l.update_lrecord_cfg (lr_cfg);
	l = l->next ();
	}*/
}
#endif
// ----------------------------------------------------------------------------
int MethodTrace::count_ = -1;
MethodTrace::MethodTrace (Logger *log, 
		const char *file, 
		const char *func, 
		int line)
: line_ (line)
, file_ (file)
, func_ (func)
, log_ (log)
{
	this->init ();
	MethodTrace::count_++;
	char *p = spaces_;
	for (int i = 0; i < count_ * 2; i++)
		++p;
	*p = '\0';
	log_->log (LL_TRACE, "%s(%d)calling `%s` in file `%s` on line `%d`",
			spaces_,
			MethodTrace::count_,
			func_, file_, line_);
}
MethodTrace::~MethodTrace ()
{
	this->init ();
	char *p = this->spaces_;
	for (int i = 0; i < count_ * 2; i++)
		++p;
	*p = '\0';
	this->log_->log (LL_TRACE, "%s(%d)leaving `%s`", 
			this->spaces_,
			MethodTrace::count_,  func_);
	MethodTrace::count_--;
}


